package bar.dark.lee.setting.sms;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import bar.dark.lee.BDConstents;
import bar.dark.lee.R;
import bar.dark.lee.SuperActivity;
import bar.dark.lee.common.BDUtil;
import bar.dark.lee.common.DBSQLite;
import bar.dark.lee.common.DBSQLite.ParseSelectData;
import bar.dark.lee.setting.TipInfoActivity;
import bar.dark.lee.setting.TipsActivity;

public class SMSSettingActivity extends SuperActivity implements OnClickListener
{
	public static final String CARD_NAME = "card_name";
	public static final String MEMO = "memo";
	public static final String DATE = "data";
	public static final String MONEY = "money";
	
	private ArrayList<AllCardData> mArrayCard = new ArrayList<AllCardData>();
	private DBSQLite sql;
	private AlertDialog dialog;
	private String linkCardType;
	private String where;
	private String kind;
	
	private String date;
	private String money;
	
	private int smsType;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sms_setting_activity);
		
		sql = DBSQLite.getInstance(this);
		
		where = getIntent().getStringExtra("where");
		
		initFindView();
		initData();
		
		if (where.equalsIgnoreCase("InsertOutActivity"))
		{
			setTitle("지출 문자 문자 입력");
		}
		else if (where.equalsIgnoreCase("InsertOutActivity"))
		{
			setTitle("수입 문자 문자 입력");
		}
		else 
		{
			setTitle("문자 자동화 추가");
		}
		
//		InputData data = new InputData();
//		data = getIntent().getParcelableExtra("message");
		
	}
	
	private void initFindView() {
		findViewById(R.id.smsSetting_btnCardLink).setOnClickListener(this);
		findViewById(R.id.smsSetting_btnOk).setOnClickListener(this);
	}
	
	private void initData() {
		
		if (where.equalsIgnoreCase("InsertOutActivity")) {
			date = getIntent().getStringExtra("date");
			String body = getIntent().getStringExtra("body");
			kind = getIntent().getStringExtra("kind");
			
			if (kind == null)
			{
				BDUtil.showDialogOK(superContext, "오류", "죄송합니다. 데이터 오류입니다.", new DialogInterface.OnClickListener()
				{
					@Override
					public void onClick(DialogInterface dialog, int which)
					{
						BDUtil.btnEnable = true;
						BDUtil.asyncFlag = 0;
						
						finish();
					}
				});
			}
			
			//SMSParser2 parser2 = new SMSParser2(body);
			Parser parser2 = null;
			try {
				parser2 = new Parser(body);
			} catch (Exception e)
			{
				Toast.makeText(superContext, "문자 분석중에 문제가 생겼습니다. 개발자에게 메일을 보내주세요.", Toast.LENGTH_SHORT).show();
				return;
			} finally
			{
				if (parser2 == null)
				{
					Toast.makeText(superContext, "문자 분석중에 문제가 생겼습니다. 개발자에게 메일을 보내주세요.", Toast.LENGTH_SHORT).show();
					return;
				}
			}
			if (date != null && date.length() > 0)
			{
				parser2.setDate(date);
			}
			InputData data = new InputData();
			data = parser2.messageParsing();
			smsType = parser2.getSmsType();
			if ( data == null ||
					( (data.getMemo1() == null) || (data.getMemo1().equalsIgnoreCase("")) 
					&& ( (data.getMemo2() == null) || (data.getMemo2().equalsIgnoreCase("")) )
					&& ( (data.getMemo3() == null) || (data.getMemo3().equalsIgnoreCase("")) )
					&& ( (data.getMemo4() == null) || (data.getMemo4().equalsIgnoreCase("")) ) )
					|| !parser2.getAvailable()	  )
			{
				BDUtil.showToast(this, "지원하지 않는 문자 형식입니다..");
				return;
//				SMSParser parser = new SMSParser(body);
//				data = parser.messageParsing();
//				smsType = parser.getSmsType();
//				
//				if (data == null || !parser.getAvailable())
//				{
////					BDUtil.showDialog(SMSActivity.this, "연동불가문자", "지원하지 않는 문자 형식입니다.");
//					BDUtil.showToast(this, "지원하지 않는 문자 형식입니다..");
//					return;
//				}
//				
//				Toast.makeText(superContext, "정확하게 등록이 된 문자가 아닙니다. 해당 문자를 개발자에게 보내주시면 더 정확하게 사용이 가능합니다.", Toast.LENGTH_LONG).show();
			}
			
			findViewById(R.id.smsSetting_viewInput).setVisibility(View.GONE);
			findViewById(R.id.smsSetting_viewResult).setVisibility(View.GONE);
			
			date = data.getDate();
			money = data.getMoney();
			
			if (data.getMemo1().equalsIgnoreCase(data.getMemo2()) && data.getMemo1().equalsIgnoreCase(data.getMemo3()) && 
					data.getMemo1().equalsIgnoreCase(data.getMemo4()))
			{
				((RadioButton)findViewById(R.id.smsSetting_radioMemo1)).setText(data.getMemo1());
				((RadioButton)findViewById(R.id.smsSetting_radioMemo1)).setChecked(true);
				((RadioButton)findViewById(R.id.smsSetting_radioMemo2)).setVisibility(View.GONE);
				((RadioButton)findViewById(R.id.smsSetting_radioMemo3)).setVisibility(View.GONE);
				((RadioButton)findViewById(R.id.smsSetting_radioMemo4)).setVisibility(View.GONE);
			}
			else
			{
				((RadioButton)findViewById(R.id.smsSetting_radioMemo1)).setText(data.getMemo1());
				((RadioButton)findViewById(R.id.smsSetting_radioMemo2)).setText(data.getMemo2());
				((RadioButton)findViewById(R.id.smsSetting_radioMemo3)).setText(data.getMemo3());
				((RadioButton)findViewById(R.id.smsSetting_radioMemo4)).setText(data.getMemo4());
			}
			
			((Button)findViewById(R.id.smsSetting_btnCardLink)).setText("보유 " + kind + " 보기");
			((Button)findViewById(R.id.smsSetting_btnOk)).setText("완료");
			
		} 
		else if (where.equalsIgnoreCase("InsertInActivity")) {
			date = getIntent().getStringExtra("date");
			String body = getIntent().getStringExtra("body");
			kind = getIntent().getStringExtra("kind");
			
			if (kind == null)
			{
				BDUtil.showDialogOK(superContext, "오류", "죄송합니다. 데이터 오류입니다.", new DialogInterface.OnClickListener()
				{
					@Override
					public void onClick(DialogInterface dialog, int which)
					{
						BDUtil.btnEnable = true;
						BDUtil.asyncFlag = 0;
						
						finish();
					}
				});
			}
			
			//SMSParser2 parser2 = new SMSParser2(body);
			Parser parser2 = null;
			try {
				parser2 = new Parser(body);
			} catch (Exception e)
			{
				Toast.makeText(superContext, "문자 분석중에 문제가 생겼습니다. 개발자에게 메일을 보내주세요.", Toast.LENGTH_SHORT).show();
				return;
			} finally
			{
				if (parser2 == null)
				{
					Toast.makeText(superContext, "문자 분석중에 문제가 생겼습니다. 개발자에게 메일을 보내주세요.", Toast.LENGTH_SHORT).show();
					return;
				}
			}
			if (date != null && date.length() > 0)
			{
				parser2.setDate(date);
			}
			InputData data = new InputData();
			data = parser2.messageParsing();
			smsType = parser2.getSmsType();
			
			if ( data == null ||
					( (data.getMemo1() == null) || (data.getMemo1().equalsIgnoreCase("")) 
					&& ( (data.getMemo2() == null) || (data.getMemo2().equalsIgnoreCase("")) )
					&& ( (data.getMemo3() == null) || (data.getMemo3().equalsIgnoreCase("")) )
					&& ( (data.getMemo4() == null) || (data.getMemo4().equalsIgnoreCase("")) ) )
					|| !parser2.getAvailable()	  )
			{
				BDUtil.showToast(this, "지원하지 않는 문자 형식입니다..");
				return;
//				SMSParser parser = new SMSParser(body);
//				data = parser.messageParsing();
//				smsType = parser.getSmsType();
//				
//				if (data == null || !parser.getAvailable())
//				{
////					BDUtil.showDialog(SMSActivity.this, "연동불가문자", "지원하지 않는 문자 형식입니다.");
//					BDUtil.showToast(this, "지원하지 않는 문자 형식입니다..");
//					return;
//				}
//				
//				Toast.makeText(superContext, "정확하게 등록이 된 문자가 아닙니다. 해당 문자를 개발자에게 보내주시면 더 정확하게 사용이 가능합니다.", Toast.LENGTH_LONG).show();
			}
			
			findViewById(R.id.smsSetting_viewInput).setVisibility(View.GONE);
			findViewById(R.id.smsSetting_viewResult).setVisibility(View.GONE);
			
			date = data.getDate();
			money = data.getMoney();
			
			if (data.getMemo1().equalsIgnoreCase(data.getMemo2()) && data.getMemo1().equalsIgnoreCase(data.getMemo3()) && 
					data.getMemo1().equalsIgnoreCase(data.getMemo4()))
			{
				((RadioButton)findViewById(R.id.smsSetting_radioMemo1)).setText(data.getMemo1());
				((RadioButton)findViewById(R.id.smsSetting_radioMemo1)).setChecked(true);
				((RadioButton)findViewById(R.id.smsSetting_radioMemo2)).setVisibility(View.GONE);
				((RadioButton)findViewById(R.id.smsSetting_radioMemo3)).setVisibility(View.GONE);
				((RadioButton)findViewById(R.id.smsSetting_radioMemo4)).setVisibility(View.GONE);
			}
			else
			{
				((RadioButton)findViewById(R.id.smsSetting_radioMemo1)).setText(data.getMemo1());
				((RadioButton)findViewById(R.id.smsSetting_radioMemo2)).setText(data.getMemo2());
				((RadioButton)findViewById(R.id.smsSetting_radioMemo3)).setText(data.getMemo3());
				((RadioButton)findViewById(R.id.smsSetting_radioMemo4)).setText(data.getMemo4());
			}
			
			((Button)findViewById(R.id.smsSetting_btnCardLink)).setText("보유 " + kind + " 보기");
			((Button)findViewById(R.id.smsSetting_btnOk)).setText("완료");
			
		}  
		
		else if (where.equalsIgnoreCase("SMSActivity")) {
			date = getIntent().getStringExtra("date");
			String address = getIntent().getStringExtra("address");
			String body = getIntent().getStringExtra("body");
			
			//SMSParser2 parser2 = new SMSParser2(body);
			Parser parser2 = null;
			try {
				parser2 = new Parser(body);
			} catch (Exception e)
			{
				Toast.makeText(superContext, "문자 분석중에 문제가 생겼습니다. 개발자에게 메일을 보내주세요.", Toast.LENGTH_SHORT).show();
				return;
			} finally
			{
				if (parser2 == null)
				{
					Toast.makeText(superContext, "문자 분석중에 문제가 생겼습니다. 개발자에게 메일을 보내주세요.", Toast.LENGTH_SHORT).show();
					return;
				}
			}
			if (date != null && date.length() > 0)
			{
				parser2.setDate(date);
			}
			InputData data = new InputData();
			data = parser2.messageParsing();
			smsType = parser2.getSmsType();
			
			if (data == null ||
					( (data.getMemo1() == null) || (data.getMemo1().equalsIgnoreCase("")) 
					&& ( (data.getMemo2() == null) || (data.getMemo2().equalsIgnoreCase("")) )
					&& ( (data.getMemo3() == null) || (data.getMemo3().equalsIgnoreCase("")) )
					&& ( (data.getMemo4() == null) || (data.getMemo4().equalsIgnoreCase("")) ) )
					|| !parser2.getAvailable()	  )
			{
				BDUtil.showToast(this, "지원하지 않는 문자 형식입니다..");
				return;
//				SMSParser parser = new SMSParser(body);
//				data = parser.messageParsing();
//				smsType = parser.getSmsType();
//				
//				if (data == null || !parser.getAvailable())
//				{
////					BDUtil.showDialog(SMSActivity.this, "연동불가문자", "지원하지 않는 문자 형식입니다.");
//					BDUtil.showToast(this, "지원하지 않는 문자 형식입니다..");
//					return;
//				}
//				
//				Toast.makeText(superContext, "정확하게 등록이 된 문자가 아닙니다. 해당 문자를 개발자에게 보내주시면 더 정확하게 사용이 가능합니다.", Toast.LENGTH_LONG).show();
			}
			
			if (data != null) 
			{
				if (smsType == BDConstents.IN_SMS || smsType == BDConstents.IN_ATM_SMS)
				{
					((TextView)findViewById(R.id.smsSetting_textDateTitle)).setText("입금날짜");
					((TextView)findViewById(R.id.smsSetting_textMoneyTitle)).setText("입금금액");
				}
				else if (smsType == BDConstents.OUT_ATM_SMS)
				{
					((TextView)findViewById(R.id.smsSetting_textDateTitle)).setText("출금날짜");
					((TextView)findViewById(R.id.smsSetting_textMoneyTitle)).setText("출금금액");
				}
				
				((TextView)findViewById(R.id.smsSetting_textAddress)).setText(address);
				((TextView)findViewById(R.id.smsSetting_textBody)).setText(body);
				((TextView)findViewById(R.id.smsSetting_textName)).setText(data.getName());
				((TextView)findViewById(R.id.smsSetting_textDate)).setText(data.getDate());
				((TextView)findViewById(R.id.smsSetting_textMoney)).setText(data.getMoney());
				
				if (data.getMemo1().equalsIgnoreCase(data.getMemo2()) && data.getMemo1().equalsIgnoreCase(data.getMemo3()) && 
						data.getMemo1().equalsIgnoreCase(data.getMemo4()))
				{
					((RadioButton)findViewById(R.id.smsSetting_radioMemo1)).setText(data.getMemo1());
					((RadioButton)findViewById(R.id.smsSetting_radioMemo2)).setVisibility(View.GONE);
					((RadioButton)findViewById(R.id.smsSetting_radioMemo3)).setVisibility(View.GONE);
					((RadioButton)findViewById(R.id.smsSetting_radioMemo4)).setVisibility(View.GONE);
				}
				else
				{
					((RadioButton)findViewById(R.id.smsSetting_radioMemo1)).setText(data.getMemo1());
					((RadioButton)findViewById(R.id.smsSetting_radioMemo2)).setText(data.getMemo2());
					((RadioButton)findViewById(R.id.smsSetting_radioMemo3)).setText(data.getMemo3());
					((RadioButton)findViewById(R.id.smsSetting_radioMemo4)).setText(data.getMemo4());
				}
			}
			else
			{
				BDUtil.showDialogFinish(superContext, "오류", "자동화 추가 실패입니다. 다른 문자로 시도해주세요.");
			}
			
		}
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.smsSetting_btnCardLink:
				mArrayCard.clear();
				
				if (where.equalsIgnoreCase("InsertOutActivity") || where.equalsIgnoreCase("InsertInActivity")) 
				{
					if (kind.equalsIgnoreCase("체크카드")) 
					{
						getCheckCardData();
					} 
					else if (kind.equalsIgnoreCase("신용카드")) 
					{
						getCreditCardData();
					}
					else if (kind.equalsIgnoreCase("통장")) 
					{
						getBankbookData();
					}
				} 
				else if (where.equalsIgnoreCase("SMSActivity")) 
				{
					getCheckCardData();
					getCreditCardData();
					getBankbookData();
				}
				
				if (mArrayCard.size() > 0)
				{
					View view = LayoutInflater.from(this).inflate(R.layout.sms_dialog2, null);
					ListView listView = (ListView) view.findViewById(R.id.smsDialog2_listView);
					CardAdapter adapter = new CardAdapter(this, R.layout.list_item_sms3, mArrayCard); 
					listView.setAdapter(adapter);
					listView.setOnItemClickListener(adapter);
					
					adapter.setNotifyOnChange(true);

					AlertDialog.Builder builder = new AlertDialog.Builder(this);
					builder.setView(view);
					builder.setNegativeButton("취소", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
						}
					});
					dialog = builder.create();
					dialog.show();
				}
				else
				{
					BDUtil.showDialogManager(superContext, "카드가 하나도 없습니다.");
				}
				
				break;
			case R.id.smsSetting_btnOk:
				
				if (((TextView)findViewById(R.id.smsSetting_textMyCardName)).getText().toString().equalsIgnoreCase("-")) {
					BDUtil.showDialog(this, "연동카드누락", "문자와 연동될 카드를 선택해주세요.");
					return;
				}
				
				if (where.equalsIgnoreCase("InsertOutActivity") || where.equalsIgnoreCase("InsertInActivity") ) {
					String memo = "";
					switch (((RadioGroup)findViewById(R.id.smsSetting_radioGroup)).getCheckedRadioButtonId()) {
						case R.id.smsSetting_radioMemo1:
							memo = ((RadioButton)findViewById(R.id.smsSetting_radioMemo1)).getText().toString();
						break;
						case R.id.smsSetting_radioMemo2:
							memo = ((RadioButton)findViewById(R.id.smsSetting_radioMemo2)).getText().toString();					
						break;
						case R.id.smsSetting_radioMemo3:
							memo = ((RadioButton)findViewById(R.id.smsSetting_radioMemo3)).getText().toString();
						break;
						case R.id.smsSetting_radioMemo4:
							memo = ((RadioButton)findViewById(R.id.smsSetting_radioMemo4)).getText().toString();
						break;
						default:
						break;
					}
					
					Intent intent = new Intent();
					intent.putExtra(CARD_NAME, ((TextView)findViewById(R.id.smsSetting_textMyCardName)).getText().toString());
					intent.putExtra(DATE, date);
					intent.putExtra(MONEY, money);
					intent.putExtra(MEMO, memo);
					setResult(Activity.RESULT_OK, intent);
					finish();
					
				} else if (where.equalsIgnoreCase("SMSActivity")) {
					if (((TextView)findViewById(R.id.smsSetting_textName)).getText().toString().length()==0 ||
							((TextView)findViewById(R.id.smsSetting_textAddress)).getText().toString().length()==0) {
						BDUtil.showDialog(this, "적용불가문자", "죄송합니다. 적용할 수 없는 문자입니다. 개발자에게 해당 문자메세지를 메일로 보내주세요.");
						return;
					}
					
					if (((TextView)findViewById(R.id.smsSetting_textMyCardName)).getText().toString().equalsIgnoreCase("-")) {
						BDUtil.showDialog(this, "연동카드누락", "문자와 연동될 카드를 선택해주세요.");
						return;
					}
			
					new ShowDialog().execute(BDUtil.asyncFlag, 1);
				}
			break;

			default:
			break;
		}
	}
	
	private void saveData() {
		HashMap<String, String> map = new HashMap<String, String>();
		map.put("name", ((TextView)findViewById(R.id.smsSetting_textName)).getText().toString());
//		map.put("date", ((TextView)findViewById(R.id.smsSetting_textDate)).getText().toString());
		map.put("address", ((TextView)findViewById(R.id.smsSetting_textAddress)).getText().toString());
		map.put("message", ((TextView)findViewById(R.id.smsSetting_textBody)).getText().toString());
		map.put("messagecardname", ((TextView)findViewById(R.id.smsSetting_textName)).getText().toString());
		map.put("linkcardname", ((TextView)findViewById(R.id.smsSetting_textMyCardName)).getText().toString());
		map.put("linkcardtype", linkCardType);
		switch (((RadioGroup)findViewById(R.id.smsSetting_radioGroup)).getCheckedRadioButtonId()) {
			case R.id.smsSetting_radioMemo1:
				map.put("parser", "1");
			break;
			case R.id.smsSetting_radioMemo2:
				map.put("parser", "2");
			break;
			case R.id.smsSetting_radioMemo3:
				map.put("parser", "3");
			break;
			case R.id.smsSetting_radioMemo4:
				map.put("parser", "4");
			break;
			default:
			break;
		}
		
		SharedPreferences pref = getSharedPreferences("pref", Activity.MODE_PRIVATE);
		int radioAutoCheck = pref.getInt("smsRadioGroup", 1);
		if (radioAutoCheck == 1)
		{
			SharedPreferences.Editor editor = pref.edit();
			editor.putInt("smsRadioGroup", 2);
			editor.commit();
		}
		
		try {
			sql.insert(this, map, "sms");
		} catch (Exception e) {
			e.printStackTrace();
			BDUtil.showToast(getApplicationContext(), "입력 실패");
			return;
		}
	}
	
	/**
	 * 신용카드 데이터 가져온다
	 */
	private void getCreditCardData() {
		String query = "";
		
		query = "SELECT id, name FROM credit_card"; 
		
		sql.selectMapTile(this, query, new ParseSelectData()
		{
			@Override
			public void resultData(Cursor c)
			{
				int count = c.getCount();
				c.moveToFirst();
				
				int idIndex = c.getColumnIndex("id");
				int nameIndex = c.getColumnIndex("name");
				
				for (int i = 0; i < count; i++)
				{
					AllCardData data = new AllCardData();
					data.setId(c.getInt(idIndex) + "");
					data.setName(c.getString(nameIndex));
					data.setCardType("3");
					mArrayCard.add(data);
					c.moveToNext();
				}
			}
		});
	}
	
	/**
	 * 체크카드 데이터 가져온다
	 */
	private void getCheckCardData() {
		String query = "";
		
		query = "SELECT id, name FROM check_card"; 
		
		sql.selectMapTile(this, query, new ParseSelectData()
		{
			@Override
			public void resultData(Cursor c)
			{
				int count = c.getCount();
				c.moveToFirst();
				
				int idIndex = c.getColumnIndex("id");
				int nameIndex = c.getColumnIndex("name");
				
				for (int i = 0; i < count; i++)
				{
					AllCardData data = new AllCardData();
					data.setId(c.getInt(idIndex) + "");
					data.setName(c.getString(nameIndex));
					data.setCardType("4");
					mArrayCard.add(data);
					c.moveToNext();
				}
			}
		});
	}
	
	/**
	 * 통장 데이터 가져온다
	 */
	private void getBankbookData() {
		String query = "";
		
		query = "SELECT id, name FROM ( SELECT b.id id, b.name name FROM bankbook b, bankbook_detail d WHERE b.id = d.linkid and count > 0 ORDER BY count) " +
				"UNION ALL " +
				"SELECT id, name FROM ( SELECT b.id id, b.name name FROM bankbook b, bankbook_detail d WHERE b.id = d.linkid and count < 1 ORDER BY count);";; 
		
		sql.selectMapTile(this, query, new ParseSelectData()
		{
			@Override
			public void resultData(Cursor c)
			{
				int count = c.getCount();
				c.moveToFirst();
				
				int idIndex = c.getColumnIndex("id");
				int nameIndex = c.getColumnIndex("name");
				
				for (int i = 0; i < count; i++)
				{
					AllCardData data = new AllCardData();
					data.setId(c.getInt(idIndex) + "");
					data.setName(c.getString(nameIndex));
					data.setCardType("1");
					mArrayCard.add(data);
					c.moveToNext();
				}
			}
		});
	}
	
	private class CardAdapter extends ArrayAdapter<AllCardData> implements OnItemClickListener 
	{
		Context context;
		int viewId;
		ArrayList<AllCardData> items;
		
		public CardAdapter(Context context, int textViewResourceId, ArrayList<AllCardData> objects) {
			super(context, textViewResourceId, objects);
			this.context = context;
			this.viewId = textViewResourceId;
			this.items = objects;
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view = convertView;
			
			if (view == null) {
				view = LayoutInflater.from(context).inflate(viewId, null);
			}
			
			AllCardData data = items.get(position);
			
			if (data.getCardType().equals("3"))
			{
				((TextView) view.findViewById(R.id.listSMS3Type)).setText("신용카드 : ");
			}
			else if (data.getCardType().equals("4"))
			{
				((TextView) view.findViewById(R.id.listSMS3Type)).setText("체크카드 : ");
			}
			else if (data.getCardType().equals("1"))
			{
				((TextView) view.findViewById(R.id.listSMS3Type)).setText("통      장 : ");
			}
			
			((TextView) view.findViewById(R.id.listSMS3Name)).setText(data.getName());
			
			return view;
		}
		
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
			((TextView) findViewById(R.id.smsSetting_textMyCardName)).setText(items.get(arg2).getName());
			linkCardType = items.get(arg2).getCardType();
			if (dialog != null) {
				dialog.dismiss();
				dialog = null;
			}
		}
	}
	
	@Override
	protected void tipButton() {
		Intent intent = new Intent(this, TipInfoActivity.class);
		intent.putExtra(TipsActivity.TIP_REQUEST_KEY, TipsActivity.TIP_AUTOMASSAGE);
		startActivity(intent);
	}
	
	/**
	 * 다이얼로그 뛰우기 
	 */
	private class ShowDialog extends AsyncTask<Integer, Integer, Integer>{
		ProgressDialog dialog = null;
		
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			
			if (BDUtil.btnEnable)
			{
				BDUtil.btnEnable = false;
				BDUtil.asyncFlag = -1;
				
				dialog = new ProgressDialog(SMSSettingActivity.this);
				dialog.setCancelable(false);
				dialog.setMessage("Loading...");
				dialog.show();
			}
		}
		
		@Override
		protected Integer doInBackground(Integer... arg0) {
			
			if (arg0[0] == 0) {
				if (arg0[1] == 1)
				{
					try
					{
						sql.beginTansaction(superContext);
						
						saveData();
						
						sql.setTransactionSuccessful();
					} catch (Exception e)
					{
						return -1;
					} finally {
						try 
						{
							sql.endTransaction(superContext);
						} catch (Exception e) 
						{
							return -1;
						}
					}
					
					return arg0[1];
				}
				return arg0[0];
			} else 
				return arg0[0];
		}

		@Override
		protected void onPostExecute(Integer result) {
			super.onPostExecute(result);
			
			if (dialog != null) {
				dialog.dismiss();
			}
			
			if (result == -1)
			{
				BDUtil.btnEnable = true;
				BDUtil.asyncFlag = 0;
				
				BDUtil.showDialog(superContext, "오류", "데이터 저장에 실패했습니다.");
			}
			else if (result == 1)
			{
				BDUtil.showDialogFinish(SMSSettingActivity.this, "알림", "문자 자동화 추가에 성공하였습니다.");
			}
		}
	}
}
